var util = require('../../utils/util.js');
Component({
  options: {
    // 在组件定义时的选项中启用多slot支持
    multipleSlots: true,
  },
  /**
   * 组件的属性列表
   */
  properties: {
    // 初始化日期
    initValue: {
      type: String,
      value: "",
    },
    // 父组件传递过来的数据列表
    items: {
      type: Array,
      value: [],
    },
    Values: {
      type: Number,
      value: 0,
    },
    flag: {
      type: Boolean,
      value: true
    },
    placeholder: {
      type: String,
      value: ''
    }
    ,
    showPickerCode: {
      type: String,
    }
  },
  observers: {
    'showPickerCode': function (showPickerCode) {
      if (showPickerCode != this.data.PickerCode) {
        this.setData({ setValues: [], PickerCode: showPickerCode, searchValue: "" })
      }
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    //控制picker的显示与隐藏
    flag: true,
    // 用户输入的item关键词
    searchValue: "",
    // 滚动选择的item
    setValues: [],
    // 滚动选择的item索引
    selectIndex: 0,
    PickerCode: 'car'

  },
  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * @name: 搜索item
     * @date: 20211129
     */
    search (e) {
      console.log('触发search事件', e)
      let self = this;
      self.triggerEvent("handleSearch", e.detail)
    },
    /**
     * @name: 隐藏picker
     * @date: 20211129
     */
    hiddeDatePicker () {
      let self = this;
      self.setData({
        searchValue: "",
      });
      self.triggerEvent("hiddPicker")
    },
    /**
     * @name: 展示picker
     * @date: 20211129
     */
    showDatePicker () {
      let self = this;
      self.setData({
        flag: !self.data.flag,
      });
      self.getItems();
    },
    /**
     * @name: 选择好item后，点击确定
     * @date: 20211129
     */
    confirm () {
      let self = this;
      // 获取用户选择的item
      console.log(self.data.selectIndex, "self.data.selectIndex", this.data);
      let item = self.data.items[self.data.selectIndex]
        ? self.data.items[self.data.selectIndex]
        : self.data.items[0];
      let selectIndex = self.data.selectIndex;
      // 通过发送自定义事件把用户选择的item传递到父组件
      console.log(item, "itemitem");
      //取name 是因为从父组件传来的数据 格式已经处理成name:xxxxx
      if (item) {
        self.triggerEvent("confirm", {
          name: item,
          toback: item.toback, //传到后台的数据
          selectIndex: selectIndex,
        });
        self.setData({
          searchValue: "",
        });
        return
      }
      wx.showToast({
        title: '请选择你要确定的数据',
        icon: 'none',
        duration: 1500,
      });

    },
    /**
     * @name: 用户滚动picker时，获取滚动选择的索引
     * @author: camellia
     * @date: 20211129
     */
    bindChange (e) {
      let self = this;
      console.log(e, "eee");
      self.setData({
        // 用户选择的item索引
        selectIndex: e.detail.value[0],
      });
    },
    /**
     * @name: 获取初始化信息
     * @date: 20211130
     */
    getItems (e) {
      let self = this;
      console.log(self.data.items, "获取父组件的item");
      console.log(self.data.initValue, "initValue");
      console.log(self.data.Values, "Values");
      if (self.data.items[0].name) {
        self.setData({
          setValues: [self.data.Values],
        });
      } else {
        //如果没有数据
        self.setData({
          setValues: [0],
        });
        console.log(self.data.setValues, "setValuessetValues")
      }
    },
  },
});